﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<AnalisisDeRiesgos.Models.Danger.EditDangerModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Editar Peligro
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
    <script src="../../Scripts/riskcalculator.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#Management").change(function () {
                $.getJSON("/Danger/GetProcessByManagementId/" + $("#Management > option:selected").attr("value"), function (data) {
                    var items = "";
                    $.each(data, function (i, state) {
                        items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                    });
                    $("#Process").html(items);

                    $.getJSON("/Danger/GetAreaByProcessId/" + $("#Process > option:selected").attr("value"), function (data) {
                        var items = "";
                        $.each(data, function (i, state) {
                            items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                        });
                        $("#Area").html(items);
                        $.getJSON("/Danger/GetActivityByAreaId/" + $("#Area > option:selected").attr("value"), function (data) {
                            var items = "";
                            $.each(data, function (i, state) {
                                items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                            });
                            $("#Activity").html(items);
                        });
                    });
                });
            });

            $("#Process").change(function () {
                $.getJSON("/Danger/GetAreaByProcessId/" + $("#Process > option:selected").attr("value"), function (data) {
                    var items = "";
                    $.each(data, function (i, state) {
                        items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                    });
                    $("#Area").html(items);
                    $.getJSON("/Danger/GetActivityByAreaId/" + $("#Area > option:selected").attr("value"), function (data) {
                        var items = "";
                        $.each(data, function (i, state) {
                            items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                        });
                        $("#Activity").html(items);
                    });
                });
            });

            $("#Area").change(function () {
                $.getJSON("/Danger/GetActivityByAreaId/" + $("#Area > option:selected").attr("value"), function (data) {
                    var items = "";
                    $.each(data, function (i, state) {
                        items += "<option value='" + state.Value + "'>" + state.Text + "</option>";
                    });
                    $("#Activity").html(items);
                });
            });

            $("#Frequence").change(function () {
                calculateRisk("#Frequence", "#ExposedNumber", "#Controls", "#Severity", "#KFactor", "#probability", "#risklevel", "#significant", "#term", "#ExpirationDate", "#actual", "#newval", "#newlevel");
            });

            $("#ExposedNumber").change(function () {
                calculateRisk("#Frequence", "#ExposedNumber", "#Controls", "#Severity", "#KFactor", "#probability", "#risklevel", "#significant", "#term", "#ExpirationDate", "#actual", "#newval", "#newlevel");
            });

            $("#Controls").change(function () {
                calculateRisk("#Frequence", "#ExposedNumber", "#Controls", "#Severity", "#KFactor", "#probability", "#risklevel", "#significant", "#term", "#ExpirationDate", "#actual", "#newval", "#newlevel");
            });

            $("#Severity").change(function () {
                calculateRisk("#Frequence", "#ExposedNumber", "#Controls", "#Severity", "#KFactor", "#probability", "#risklevel", "#significant", "#term", "#ExpirationDate", "#actual", "#newval", "#newlevel");
            });

            $("#KFactor").change(function () {
                calculateRisk("#Frequence", "#ExposedNumber", "#Controls", "#Severity", "#KFactor", "#probability", "#risklevel", "#significant", "#term", "#ExpirationDate", "#actual", "#newval", "#newlevel");
            });

            calculateRisk("#Frequence", "#ExposedNumber", "#Controls", "#Severity", "#KFactor", "#probability", "#risklevel", "#significant", "#term", "#dateAux", "#actual", "#newval", "#newlevel");
        });

        $().ready(function () {
            $.get("/Action/Index/<%: Model.DangerId %>", function (htmlResult) {                
                $("#ActionList").remove();
                $("#actioncontainer").append(htmlResult);
            });
            
            $("#SearchRiskButton").click(function () {
                $.get("/Risk/SearchRisk", function (htmlResult) {
                    $("#SearchRisk").remove();
                    $("#container").append(htmlResult);
                    $("#SearchRisk").dialog();
                });
                return false;
            });

            $("#registerActionLink").click(function () {
                $.get("/Action/Create/<%: Model.DangerId %>" , function (htmlResult) {
                    $("#RegisterAction").remove();
                    $("#container").append(htmlResult);
                    $("#RegisterAction").dialog({ minWidth: 600 });
                });
                return false;
            });

            $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
        });
    </script>
    <div id="container">
        <h2>Editar peligro</h2>
        <input type="hidden" id="dateaux" />

        <% using (Html.BeginForm()) {%>
            <%: Html.ValidationSummary(true) %>

            <fieldset>
                <legend>Campos</legend>

                <%: Html.HiddenFor(model => model.DangerId) %>
                
                <table>
                    <tr>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.Management) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.DropDownListFor(model => model.Management, Model.Managements) %>
                                <%: Html.ValidationMessageFor(model => model.Management) %>
                            </div>
                        </td>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.Process) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.DropDownListFor(model => model.Process, Model.Processes) %>
                                <%: Html.ValidationMessageFor(model => model.Process) %>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.Area) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.DropDownListFor(model => model.Area, Model.Areas) %>
                                <%: Html.ValidationMessageFor(model => model.Area) %>
                            </div>
                        </td>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.Activity) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.DropDownListFor(model => model.Activity, Model.Activitys) %>
                                <%: Html.ValidationMessageFor(model => model.Activity) %>
                            </div>
                        </td>
                    </tr>
                </table>
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Danger) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextAreaFor(model => model.Danger, 5, 50, null) %>
                    <%: Html.ValidationMessageFor(model => model.Danger) %>
                </div>
            </fieldset>

            <fieldset>
                <legend>Detalles riesgo</legend>
                <div style="margin:5px;">
                    <div class="editor-label">                    
                        <span style="font-weight:bold;">Codigo Riesgo</span>
                        <span id = "RiskCode">-</span>
                    </div>
                    <div class="editor-field">
                        <span style="font-weight:bold;">Detalles Riesgo</span>
                        <span id = "RiskDescription">Elige un riesgo</span>
                        <%: Html.HiddenFor(model => model.Risk) %>
                        <%: Html.ValidationMessageFor(model => model.Risk) %>
                    </div>
                    <a href="", id="SearchRiskButton">Buscar Riesgo</a>
                </div>
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.OperativeCondition) %>
                </div>
                <div class="editor-field">                
                    <%: Html.RadioButtonFor(model => model.OperativeCondition, "N", true) %> Normal
                    <%: Html.RadioButtonFor(model => model.OperativeCondition, "A", false) %> Anormal
                    <%: Html.ValidationMessageFor(model => model.OperativeCondition) %>
                </div>

                <div class="editor-label">
                    <%: Html.LabelFor(model => model.AffectedTarget) %>
                </div>
                <div class="editor-field">                
                    <%: Html.RadioButtonFor(model => model.AffectedTarget, "H", true) %> Humano
                    <%: Html.RadioButtonFor(model => model.AffectedTarget, "I", false) %> Infraestructura
                    <%: Html.ValidationMessageFor(model => model.AffectedTarget) %>
                </div>

                <table>
                    <tr>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.EspecificUbication) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.TextAreaFor(model => model.EspecificUbication, 5, 50, null) %>
                                <%: Html.ValidationMessageFor(model => model.EspecificUbication) %>
                            </div>
                        </td>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.Causes) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.TextAreaFor(model => model.Causes, 5, 50, null)%>
                                <%: Html.ValidationMessageFor(model => model.Causes) %>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.Consecuences) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.TextAreaFor(model => model.Consecuences, 5, 50, null)%>
                                <%: Html.ValidationMessageFor(model => model.Consecuences) %>
                            </div> 
                        </td>
                        <td>
                            <div class="editor-label">
                                <%: Html.LabelFor(model => model.ExistingControls) %>
                            </div>
                            <div class="editor-field">
                                <%: Html.TextAreaFor(model => model.ExistingControls, 5, 50, null)%>
                                <%: Html.ValidationMessageFor(model => model.ExistingControls) %>
                            </div>
                        </td>
                    </tr>
                </table>

                <fieldset>
                    <legend>Calculo de riesgo</legend>
                    <div>
                        Probabilidad:
                        <span id = "probability" />
                    </div>
                    <div>
                        Nivel de riesgo:
                        <span id = "risklevel" />
                    </div>
                    <div>
                        El riesgo es significativo?:
                        <span id = "significant" />
                    </div>                  
                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Frequence) %>
                    </div>

                    <div class="editor-field">
                        <%: Html.DropDownListFor(model => model.Frequence, Model.Frequences, new { @class = "riskdropdown" })%>
                        <%: Html.ValidationMessageFor(model => model.Frequence) %>
                    </div>
            
                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.ExposedNumber) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.DropDownListFor(model => model.ExposedNumber, Model.Affecteds, new { @class = "riskdropdown" })%>
                        <%: Html.ValidationMessageFor(model => model.ExposedNumber) %>
                    </div>
            
                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Controls) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.DropDownListFor(model => model.Controls, Model.ControlList, new { @class = "riskdropdown" })%>
                        <%: Html.ValidationMessageFor(model => model.Controls) %>
                    </div>
            
                    <div class="editor-label">
                        <%: Html.LabelFor(model => model.Severity) %>
                    </div>
                    <div class="editor-field">
                        <%: Html.DropDownListFor(model => model.Severity, Model.SeverityList, new { @class = "riskdropdown" })%>
                        <%: Html.ValidationMessageFor(model => model.Severity) %>
                    </div>
                </fieldset>
            
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.PosibleControls) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextAreaFor(model => model.PosibleControls, 5, 50, null) %>
                    <%: Html.ValidationMessageFor(model => model.PosibleControls) %>
                </div>

                <div>
                    Plazo de ejecucion para las actividades
                    <span id = "term" style="font-weight:bold; font-size:120%;"/>
                </div>
            
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.ExpirationDate) %>
                </div>
                <div class="editor-field">                
                    <%: Html.TextBoxFor(model => model.ExpirationDate, new { @class = "datepicker" })%>
                    <%: Html.ValidationMessageFor(model => model.ExpirationDate) %>
                </div>
                
                <fieldset>
                    <legend>Acciones</legend>
                    Actividades:<br />
                    <div id="actioncontainer">                        
                    </div>
                    <a href="" id="registerActionLink">Registrar Accion</a>
                </fieldset>         
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Status) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Status) %>
                    <%: Html.ValidationMessageFor(model => model.Status) %>
                </div>

                <div>
                    Valor actual del riesgo
                    <span id="actual"></span>
                </div>
                <div>
                    Valor nuevo del riesgo
                    <span id="newval"></span>
                </div>

                <div class="editor-label">
                    <%: Html.LabelFor(model => model.KFactor) %>
                </div>
                <div class="editor-field">
                    <%: Html.DropDownListFor(model => model.KFactor, Model.KValuesList) %>
                    <%: Html.ValidationMessageFor(model => model.KFactor) %>
                </div> 

                <div>
                    Nuevo nivel del riesgo
                    <span id="newlevel"></span>
                </div>
                
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Inversion) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Inversion) %>
                    <%: Html.ValidationMessageFor(model => model.Inversion) %>
                </div>          
                              
            </fieldset>

            <p>
                <input type="submit" value="Guardar" />
            </p>

        <% } %>

        <div>
            <%: Html.ActionLink("Regresar a la lista", "Index") %>
        </div>
    </div>

</asp:Content>

